

package com.hazelcast.internal.partition.impl;

import com.hazelcast.internal.partition.PartitionEventListener;
import com.hazelcast.partition.MigrationListener;
import com.hazelcast.partition.ReplicaMigrationEvent;

/**
 * Wraps a migration listener and dispatches a migration event to matching method
 */
class MigrationListenerAdapter implements PartitionEventListener<ReplicaMigrationEvent> {

    static final int MIGRATION_STARTED_PARTITION_ID = -1;
    static final int MIGRATION_FINISHED_PARTITION_ID = -2;

    private final MigrationListener migrationListener;

    MigrationListenerAdapter(MigrationListener migrationListener) {
        this.migrationListener = migrationListener;
    }

    @Override
    public void onEvent(ReplicaMigrationEvent event) {
        switch (event.getPartitionId()) {
        case MIGRATION_STARTED_PARTITION_ID:
            migrationListener.migrationStarted(event.getMigrationState());
            break;
        case MIGRATION_FINISHED_PARTITION_ID:
            migrationListener.migrationFinished(event.getMigrationState());
            break;
        default:
            if (event.isSuccess()) {
                migrationListener.replicaMigrationCompleted(event);
            } else {
                migrationListener.replicaMigrationFailed(event);
            }
        }
    }

}
